<?php
/**
 * 其它费用记录
 * @Author    紫梦
 * @DateTime  2018-01-26
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\wxapp\controller;
use \think\Request;
use \think\Db;
use app\common\model\Fees as FeesModel;
use app\common\model\Cars;
use app\common\request\Oss;

class Fees extends Base
{
	public function index($page=1,$length=10,$seach='')
	{
        $model= new FeesModel;
        if(empty($seach)){
            $where = '1=1';
        }else{
            $where = [
                'car_sn|car_unique_id|car_id'=>['like','%'.$seach.'%']
                ];
        }
        try {           
            $data = $model->order('id', 'desc')
                        ->where($where)
                        ->paginate($length,false,[
                                'type'     => 'bootstrap',
                                'var_page' => 'page',
                                'page'=>$page ,
                            ]);             
        } catch (\Exception $e) {
            
            return [
                'code'=>2,
                'msg'=>$e->getMessage(),
                ];
        }
        return $data;
 
	}

    /**
     * 记录详情
     */
    public function detail($id = 1)
    {
        try {           
            $model = FeesModel::get($id);                      
            
        } catch (\Exception $e) {
            
            return [
                'code'=>2,
                'msg'=>$e->getMessage(),
                ];
        }
        return $model;
    }

    /**
     * 添加记录
     */
    public function add()
    {
        $request = Request::instance();
        $data = $request->post();
        if($data){               
        $car = Cars::get(['car_sn'=>$data['car_sn']]);
        }
        if(empty($car)){
            return [
            'code'=>2,
            'msg'=>'车牌号不存在',
            ];

        }

        $data['car_id'] = $car['car_id'];
        $data['car_unique_id'] = $car['car_unique_id'];
        $model = new FeesModel();
               
        $model->allowField(true)->save($data);
        $id = $model->id;
        if($id){
            return [
                'code'=>1,
                'id' => $id,
                'msg'=>'添加成功'
                ];
            
        }else{
            return [
                'code'=>2,
                'msg'=>'添加失败,请稍后再试'
                ];
        }

    }	

    /**
     * 编辑
     */
    public function edit()
    {
        $request = Request::instance();
        $data = $request->post();
        if($data){               
            $car = Cars::get(['car_sn'=>$data['car_sn']]);
        }
        if(empty($car)){
            return [
                'code'=>2,
                'msg'=>'车牌号不存在',
                ];

        }
        $data['car_id'] = $car['car_id'];
        $data['car_unique_id'] = $car['car_unique_id'];
        $model = new FeesModel();
        
        try {
            $model->allowField(true)->isUpdate(true)->save($data);
        } catch (\Exception $e) {
            return [
                'code'=>3,
                'msg'=>$e->getMessage(),
                ];
        } 
        return [
            'code'=>1,
            'msg'=>'更新成功'
            ];
    } 

    /**
     * 删除记录
     */
    public function delete($id)
    {
        try {
            FeesModel::destroy($id);
            
        } catch (\Exception $e) {
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
                ];
            
        }
        
        return [
            'code'=>1,
            'msg'=>'删除成功'
            ];
    }

    /**
     * 添加附件
     */
    public function add_file()
    {   
        $request = Request::instance();
        $id = $request->post('id');
        $file = $request->file('uploadfile');
        if($file){
            $oss = new Oss();
           
            $url = $oss->uploadOss($file);                    
            if($url){
                try {
                    Db::name('fees')->update(['id'=>$id,'uploadfile'=>$url]);
                    
                } catch (Exception $e) {
                    return [
                        'code'=>2,
                        'msg'=>$e->getMessage(),
                        ];
                }
            }
            
        } 
        return [
            'code'=>1,
            'msg'=>'上传成功'
            ];
    }

    /**
     * 删除附件
     */
    public function del_file($id)
    {
        
        $url = FeesModel::get($id);
        $oss = new Oss();
        if($oss->unlinkOss($url['uploadfile'])){
           
            try {
                Db::name('fees')->update(['id'=>$id,'uploadfile'=>null]);
            } catch (Exception $e) {
                return [
                    'code'=>2,
                    'msg'=>$e->getMessage(),
                    ];
            }
            
        }
        return [
            'code'=>1,
            'msg'=>'删除成功'
            ];
    }     
     
}